package day1;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.nio.CharBuffer;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import utility.JdbcUtils;


public class ClobTest {
	public static void main(String[] args) throws Exception {
		//readFile();
		//create();
		read();
	
	}
	
	public static void readFile() throws IOException{
		InputStream in=ClobTest.class.getClassLoader().getResourceAsStream("clob.txt");
		Reader reader=new InputStreamReader(in,"utf-8");
		char c;
		int i=0;
		StringBuffer stringBuffer=new StringBuffer();
		while((i=reader.read())!=-1){
			System.out.println((char)i);
			stringBuffer.append((char)i);
		}
		System.out.println(stringBuffer.toString());
	}
	
	public static void create() throws Exception {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con=JdbcUtils.getConnection();
			String sql = "insert into clob_test(big_text) values (?) ";
			ps=con.prepareStatement(sql);
			InputStream in=ClobTest.class.getClassLoader().getResourceAsStream("clob.txt");
			Reader reader=new InputStreamReader(in,"utf-8");
			
			
			//ps.setCharacterStream(1, reader);
			ps.setString(1, "你好");
			int i=ps.executeUpdate();
			System.out.println("i="+i);
		}finally{
			JdbcUtils.free(rs, ps, con);
		}
	}
	
	public static void read() throws Exception {
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con=JdbcUtils.getConnection();
			String sql = "select big_text from  clob_test";
			ps=con.prepareStatement(sql);
			rs=ps.executeQuery();
			while(rs.next()){
				//String s=rs.getString(1);
				//System.out.println(s);
				Reader reader=rs.getCharacterStream(1);
				int i;
				while((i=reader.read())!=-1){
					System.out.println((char)i);
				}
				reader.close();
			}
			
		}finally{
			JdbcUtils.free(rs, ps, con);
		}
	}
	
	
}
